package com.bonc.rms.utils.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bonc.rms.utils.JDBCUtil;
import com.bonc.rms.utils.JDBCUtil.QueryResult;

public class WastageInfoUtilImpl {
	/**
	 * 获取离职员工
	 * 
	 * @return
	 */
	public static List<String>  getLeaveEmployee(Integer departmentStandardId) {

		String sql = null;
		QueryResult qr = null;
		List<String> employeeList = new ArrayList<>();
		
		List<Integer> departmentIdlist = new ArrayList<>();
		departmentIdlist = WastageInfoUtilImpl
				.getDepartmentId(departmentStandardId);
		if (departmentIdlist == null || departmentIdlist.size()==0) {
			sql = "SELECT job_number FROM talent WHERE leave_date IS NOT NULL ";
			qr = JDBCUtil.query(sql);
			ResultSet rs = qr.getResultSet();

			try {
				while (rs.next()) {
					employeeList.add(rs.getString(1));

				}

			} catch (SQLException e) {

				e.printStackTrace();
			} finally {
				qr.close();

			}
		}else{
			StringBuffer departmentId =new StringBuffer();
			departmentId = WastageInfoUtilImpl.stringUtil(departmentIdlist);
			sql = "SELECT job_number FROM talent WHERE leave_date IS NOT NULL AND department_id IN"+departmentIdlist;
			qr = JDBCUtil.query(sql);
			ResultSet rs = qr.getResultSet();

			try {
				while (rs.next()) {
					employeeList.add(rs.getString(1));

				}

			} catch (SQLException e) {

				e.printStackTrace();
			} finally {
				qr.close();

			}
		}
	
		return employeeList;

	}
	/**
	 * 获取离职员工最后一次调薪记录
	 * 
	 * @return
	 */
	public static String getRecordSalary(String employeeId ) {

		String sql = null;
		QueryResult qr = null;
		String ecordSalaryStr = null;

		sql = "SELECT adjust_date FROM salary_record WHERE  job_number=? ORDER BY adjust_date  DESC LIMIT 0,1 ";
		qr = JDBCUtil.query(sql,employeeId);
		ResultSet rs = qr.getResultSet();

		try {
			while (rs.next()) {
				ecordSalaryStr=rs.getString(1);

			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			qr.close();

		}
		return ecordSalaryStr;

	}
	/**
	 * 
	 * 获取部门Id
	 */
	
	public static List<Integer> getDepartmentId(Integer departmentId ){
		
		String sql = null;
		QueryResult qr = null;
		List<Integer> list = new ArrayList<>();

		sql = "SELECT id FROM department WHERE  parent_id=? ";
		qr = JDBCUtil.query(sql,departmentId);
		ResultSet rs = qr.getResultSet();

		try {
			while (rs.next()) {
				list.add(rs.getInt(1));

			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			qr.close();

		}
		return list;
		
		
	}
	/**
	 * 
	 * list转String
	 */
	public static StringBuffer stringUtil(List<Integer> list){
		StringBuffer str=new StringBuffer();
		str.append("(");
		for (Integer integer : list) {
			str.append(integer+",");
		}
		str.append(")");
		return str.deleteCharAt(str.length()-2);
		
	}
}
